

import { _circle, _rect, _line, _text } from './svg.js'
import { patternMap } from './pattern/index.js'
import { axisMap } from './axis/index.js'
import { fractalMap } from './fractal/index.js'
import { _polygon, _triangle, _ray, _square, _pentagon, _hexagon, _heptagon } from './polygon.js'
import { pointsPath } from './path.js'
import { _star } from './star.js'
import { _curve, isCurve } from './curve.js'
import { _spiral, _twoSpiral } from './spiral.js'
import { _maze } from './maze.js'
import { _motion } from './motion.js'
import { gridCrossPoints, gridCellPoints, polarPoints, polarGridPoints, spiralPoints, } from '../math/points/index.js'
import { _gridRect, _gridPolygon } from './gridRect.js'
import { _screen } from '../utils/index.js'
import { polarShape, spiralShape, ringShape, rayShape, gridShape, isometricShape, polygonShape } from './polarShape.js'



// 图形函数
const shapeFnMap = {
    circle: _circle,
    rect: _rect,
    line: _line,
    polygon: _polygon,
    star: _star,
    ray: _ray,
    text: _text,
    triangle: _triangle,
    square: _square,
    pentagon: _pentagon,
    hexagon: _hexagon,
    heptagon: _heptagon,
    spiral: _spiral,
    maze: _maze,
    gridRect: _gridRect,
    gridPolygon: _gridPolygon,
    twoSpiral: _twoSpiral,
    polarShape,
    spiralShape,
    ringShape,
    rayShape,
    gridShape,
    isometricShape,
    polygonShape,
    ...fractalMap,
    ...axisMap,
    ...patternMap
}
// 点阵函数
export const pointsFnMap = {
    gridCrossPoints,
    gridCellPoints,
    polarPoints, polarGridPoints, spiralPoints,

}
// 能旋转的图形
// 会自动加角度a参数
export const rotatableShape = ['polygon', 'ray', 'triangle', 'square', 'pentagon', 'hexagon', 'heptagon',
    'spiral', 'polar', 'star', 'leaf', 'gridRect', 'gridPolygon']
export const patternTypes = Object.keys(patternMap)
export const shapeTypes = Object.keys(shapeFnMap)

export const renderShape = (options, props, children) => {
    let {
        shape,
        pattern,
        motion
    } = options
    options = Object.assign({}, _screen(), options)
    let svgDom
    // 填充图案
    if (pattern) {
        props = {
            ...props,
            fill: `url(#${pattern})`
        }
    }

    if (shapeFnMap[shape]) {
        // 图形
        svgDom = shapeFnMap[shape](options, props)
    } else if (pointsFnMap[shape]) {
        // 线条路径
        let points = pointsFnMap[shape](options, props)
        svgDom = pointsPath({
            points,
            closed: false,
            broken: false
        }, {
            fill: 'none',
            stroke: 'black',
            ...props
        })
    }
    else { //shape === 'path'
        // 线条路径
        let {
            points,
            closed,
            broken
        } = options

        if (isCurve(shape)) {
            // 曲线函数
            points = _curve(points, options)
        }
        svgDom = pointsPath({
            points,
            closed,
            broken
        }, props, {}, children)
    }
    return svgDom
}